<!-- 2023年4月14日 17点02分 gpd 地址管理 -->
<template>
	<view class="AddressManagement">
		<!-- 地址栏 -->
		<view style="margin-left: 4%;margin-top: 4%;">
			<view  v-for="item in userAddressList" :key="item.id" >
				<uni-swipe-action-item  :right-options="options1" @click="bindClick($event,item)">
					<view class="AddressManagement_address">
						<p style="font-size: 12px;color:#929292;margin-top: 4%;margin-left: 4%;font-weight: bold;">{{item.addressLocationProvince+'-'+item.addressLocationCity+'-'+item.addressLocationCounty}}</p>
						<view style="width: 100%;height: 35px;">
							<p style="font-size: 16px;margin-top: 2%;margin-left: 4%;font-weight: bold;float: left;">{{item.addressDetailed}}</p><p style="width: 6%;position: absolute;left: 85%;" @click="toAddressManagementDetails(item.id)"><u-icon name="edit-pen" size="20"></u-icon></p>
						</view>
						<view style="width: 100%;height: 35px;">
							<p style="font-size: 14px;margin-top: 2%;margin-left: 4%;font-weight: bold;">{{item.addressConsignee}}<span style="font-size: 14px;color:#929292;margin-left: 2%;">{{item.addressTelephoneShift}}</span><span style="background-color: red;color: white;font-size: 14px;width: 6%;" v-if="item.addressFlag==1">默认</span></p>
							
						</view>
					</view>
				</uni-swipe-action-item>
			</view>
		</view>
			
			
			
		
		
		<button style="width: 80%;margin-top: 20%;" type="warn" @click="toAddAddressManagement">新增地址</button>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				// 获取登录后用户的详细信息
				user:'',
				// 获取登陆后用户的id
				userId:'',
				// 查询用户地址结果
				userAddressList:'',
				// 地址状态层默认false
				statusShow:false,
				// 地址状态层id
				statusId:'',
				options1: [
					{
						text: '设置默认',
						style: {
							backgroundColor: '#ffb027'
						}
					},
					{
						text: '删除地址',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			}
		},
		methods:{
			// 跳转至地址详情页面
			toAddressManagementDetails(id){
				uni.navigateTo({
					url:"/pages/my/AddressManagementDetails?id=" +id
				})
			},
			// 跳转至新增地址页面
			toAddAddressManagement(){
				uni.navigateTo({
					url:"/pages/my/AddAddressManagement"
				})
			},
			// 初始化查询用户所有的地址
			getUserAllAddress(){
				this.$myRequest({
					url:'/userAddress/getUserAllAddress',
					data:{
						userId:this.userId,
					}
				}).then(res=>{
					this.userAddressList = res.data.data
				})
			},
			// 长按地址选择地址状态
			slectAddressStatus(id){
				this.statusShow = true
				this.statusId = id
			},
			// 右滑地址状态
			bindClick(e,item){
				if(e.content.text === '删除地址'){
					var id  = item.id
					this.$myRequest({
						url:'/userAddress/delAddressByIdApplet',
						data: JSON.stringify(id),
						method: 'delete'
					}).then(res=>{
						uni.showToast({
							title: '删除成功',
							//将值设置为 success 或者直接不用写icon这个参数
							icon: 'success',
							//显示持续时间为 2秒
							duration: 2000
						})
						// 刷新
						this.getUserAllAddress()
					})
				}
				else{
					this.$myRequest({
						url:'/userAddress/updateAddressByIdApplet',
						data: JSON.stringify(item),
						method:'POST',
					}).then(res=>{
						uni.showToast({
							title: '设置默认地址成功',
							//将值设置为 success 或者直接不用写icon这个参数
							icon: 'success',
							//显示持续时间为 2秒
							duration: 2000
						})
						// 刷新
						this.getUserAllAddress()
					})
				}
			},
		},
		onLoad() {
			this.user = wx.getStorageSync("user")
			this.userId = this.user.id
			// 初始化查询用户的地址
			this.getUserAllAddress()
		}
	}
</script>

<style>
	.AddressManagement{
		width: 100%;
		height: 870px;
		overflow: hidden;
		background-color:#F5F5F5 ;
	}
	.AddressManagement_address{
		width: 98%;
		height: 100px;
		background-color: white;
		overflow: hidden;
	}
</style>